package com.strato.repository.bootstrap.activator;

import java.util.ArrayList;
import java.util.List;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.strato.base.osgi.v1_0_0.BundleUtil;
import com.strato.logging.v1_0_0.api.Log;
import com.strato.logging.v1_0_0.api.LogFactory;
import com.strato.osgi.repository.v1_0_0.BundleMeta;
import com.strato.osgi.repository.v1_0_0.Repository;

public class Activator implements BundleActivator {

	private Log log=LogFactory.getLog(Activator.class);
	
	@Override
	public void start(BundleContext bundleContext) throws Exception {
		log.info("Bootstrap started");
		List<Repository> repos = BundleUtil.getServices(bundleContext, Repository.class, null);
		for(Repository repository:repos){
			List<BundleMeta> bundleMetas = repository.getBundles();
			List<Bundle> installedBundles=new ArrayList<Bundle>();
			for(BundleMeta bundleMeta:bundleMetas){
				List<Bundle> installed = repository.installBundle(bundleMeta, bundleContext);
				installedBundles.addAll(installed);
			}
			for(Bundle bundle:installedBundles){
				BundleUtil.startBundle(bundle);
			}
		}
	}

	@Override
	public void stop(BundleContext bundleContext) throws Exception {
	}

}
